package mysql

import (
	"gorm.io/gorm"
	user "watermelon/common/proto"
)

type User struct {
	gorm.Model
	Username string `gorm:"type:varchar(20)"  json:"username"`
	Password string `gorm:"type:varchar(32)" json:"password"`
	Mobile   string `gorm:"type:char(11)" json:"mobile"`
	Url      string `gorm:"type:varchar(200)" json:"url"`
	Wx       string `gorm:"type:varchar(50)" json:"wx"`
}

func (u *User) FindUserByMobile(mobile string) (*User, error) {
	err := DB.Where("Mobile=?", mobile).Find(&u).Error
	if err != nil {
		return nil, err
	}
	return u, nil
}

func (u *User) CreateUser(in *user.UserLoginReq) (*User, error) {
	users := &User{
		Username: in.Mobile,
		Mobile:   in.Mobile,
		Url:      "D:\\seventeen\\微信图片_202412062102291.jpg",
	}
	err := DB.Create(&users).Error
	if err != nil {
		return nil, err
	}
	return users, nil
}

func (u *User) FindUserById(id int64) (*User, error) {
	err := DB.Where("id=?", id).First(&u).Error
	if err != nil {
		return nil, err
	}
	return u, nil
}

func (u *User) UpdatePassword(id int64, password string) error {
	return DB.Model(&u).Where("id=?", id).Update("password", password).Error
}
